from pandas import DataFrame

from common.pd import excel_to_db, get_tb_data, df_to_table, df_to_excel_sheet
from common.config import Contract_Excel_path
from project.drcs import create_drcs
from project.idrs import create_idrs
from common.pyside2_table.qt_wgt import btn_open_path, Th_Base
from project.once import get_info_url


# region contract


def update_tb_contract():
    """将Excel信息更新到数据库"""
    result = excel_to_db("contract", Contract_Excel_path, sheet_name="合同信息")
    return result


def update_excel_contract(df: DataFrame):
    df_to_excel_sheet(df, Contract_Excel_path, "合同信息")


# endregion


# region factory_doc
def get_all_factory_doc():
    df = get_tb_data("factory_doc")
    return df


def update_factory_doc(df):
    return df_to_table("factory_doc", df, )


# endregion


def btn_review_info(tb_view, once_id, cell, btn_name):
    path = get_info_url(once_id)

    btn_open_path(tb_view, path, cell, btn_name)


# region drcs的相关操作

# 1） 与数据库的相关操作
def tb_to_drcs_models(conditon="status <2"):
    return get_tb_data("drcs", condition=conditon)


class Th_Create_Review(Th_Base):
    def __init__(self, model_list: list = None, review_type="drcs", parent=None) -> None:
        super().__init__(parent)
        self.model_list = model_list
        self.review_type =review_type

    def run(self):
        if self.model_list:
            create_fun = create_drcs
            if self.review_type == "idrs":
                create_fun = create_idrs
            for model in self.model_list:
                create_fun(model)
            self.signal_send.emit(1)

        else:
            self.signal_send.emit(0)

# endregion
